<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="字典名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows"
              highlight-current-row v-loading="listLoading"
              @selection-change="selsChange"
              style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="sn" label="唯一标识" width="200" sortable>
			</el-table-column>
      <el-table-column prop="title" label="字典名称" width="250" :show-overflow-tooltip="true" sortable>
      </el-table-column>
			<el-table-column prop="intro" label="描述" width="250" :show-overflow-tooltip="true" sortable>
			</el-table-column>
      <el-table-column prop="status" label="是否启用" width="250" :show-overflow-tooltip="true" sortable>
      </el-table-column>
			<el-table-column label="操作" width="400">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="total,sizes,prev, pager, next"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="query.currentPage"
                     :page-sizes="[5, 10, 50, 100]"
                     :page-size="10"
                     :total="pageInfo.total"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog title="保存" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="唯一标识" prop="sn">
              <el-input v-model="addForm.sn" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字典名称" prop="value">
              <el-input v-model="addForm.title" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="是否启用" prop="key">
              <el-input v-model="addForm.status" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="描述" prop="value" type="textarea"
                          :rows="3">
              <el-input v-model="addForm.intro" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
        snNodes:[],
        // 提交表单
        addForm:{
          id:"",
          name:"",
          sn:""
        },
        // 高级搜索
				filters: {
					name: ''
				},
        // 页面
        pageInfo:{
          rows: [],
          total: 0,
        },
				listLoading: false,
        //列表选中列
				sels: [],
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入部门名称', trigger: 'blur' }
					],
          sn: [
            { required: true, message: '请输入标识', trigger: 'blur'},
          ]
				},
        // 查询体
        query:{
          currentPage:1,
          pageSize:10,
          keyword:null
        },
        data: [],
        defaultProps: {
          children: 'child',
          label: 'name',
        },
        menusProps:{
          children: 'child',
          label: 'name',
        }
			}
		},
		methods: {
      
      /**
       * 高级查询
       */
      search(){
        this.query.currentPage = 1;
        this.getDictionarys();
      },
      /**
       * 单页大小转换
       * @param val
       */
      handleSizeChange(val){
        this.query.pageSize=val;
        this.query.currentPage=1;
        this.getDictionarys();
      },
      /**
       * 页面跳转
       * @param val
       */
			handleCurrentChange(val) {
        this.query.currentPage = val;
				this.page = val;
				this.getDictionarys();
			},
      /**
       * 获取配置分页列表
       */
			getDictionarys() {
        this.query.keyword = this.filters.name;
        this.listLoading = true;
        this.$http.post("/dictionary",this.query).then(result=>{
          result = result.data;
          if(result.success){
            this.pageInfo = result.object;
          } else {
            this.$message({type:"error",message:"查询失败！"+result.message})
          }
        }).catch(() => {
          this.$message({type:"error",message:"网络失败！"})
        })
        this.listLoading = false;
			},
      /**
       * 配置删除
       * @param index
       * @param row
       */
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/dictionary/"+row.id).then((result) => {
            this.query.currentPage=1;
						this.getDictionarys();
					});
				}).catch(() => {
          this.$message({type:"warning",message:"已取消删除!"})
				});
        // 加载效果关闭
        this.listLoading = false;
			},
			// 显示编辑界面
			handleEdit: function (index, row) {
        this.addFormVisible = true;
        this.$http.get("/dictionary/"+row.id).then((result) => {
          result = result.data;
          this.addForm = result.object;
        }).catch(() => {
          this.$message({message: '网络错误!',type: 'error'});
        })
			},
			// 显示新增界面
			handleAdd: function () {
        this.clear();
				this.addFormVisible = true;
			},
			// 新增/修改
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
              console.log(this.addForm)
              this.$http.put("/dictionary",this.addForm).then(() => {
                this.query.currentPage=1;
                this.getDictionarys();
              }).catch(() => {
                this.$message({message: '网络错误!',type: 'error'});
              });
              this.addLoading = false;
              this.addFormVisible = false;
            });
					}
				});
			},
      // 批量删除选择
			selsChange: function (sels) {
				this.sels = sels;
			},
			// 部门批量删除
			batchRemove: function () {
				let ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.patch("/dictionary",ids).then(() => {
            this.query.currentPage=1;
						this.getDictionarys();
					}).catch(() => {
            this.$message({message: '网络错误!',type: 'error'});
          })
				}).catch(() => {
          this.$message({message: '已取消删除!',type: 'warning'});
				});
        // 关闭加载效果
        this.listLoading = false;
			},
      clear(){
        this.checked1 = false;
        this.checked2 = false;
        this.filterText = "";
        if (this.$refs.addForm !== undefined) this.$refs.addForm.resetFields()
        this.addForm={
          id:"",
          name:"",
          sn:""
        }
      },
    },
		mounted() {
      this.getDictionarys();
    }
	}

</script>

<style scoped>

</style>